package com.cardone.platform.cms.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.cms.dto.ArticleDto;
import com.cardone.platform.cms.service.ArticleService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
* 文章
*
* @author yaohaitao
*/
@Setter
@Slf4j
@Controller
public class ArticleController {
/**
* 文章：删除
*
* @param deleteArticle
*            文章对象
* @return 处理结果，json格式
*/
@Deprecated
@RequestMapping(value = { "/cms/article/delete.json" }, method = RequestMethod.POST)
@ResponseBody
public Callable<Map<String, Object>> deleteJson(final ArticleDto deleteArticle) {
    return () -> {
    ContextHolder.getBean(ArticleService.class).deleteByIds(deleteArticle);

    return ReturnDataUtils.newMap();
    };
    }

    /**
    * 文章：编辑
    *
    * @param findArticle
    *            文章对象
    * @return 实体对象集，json格式
    */
    @Deprecated
    @RequestMapping(value = { "/cms/article/findById.json" })
    @ResponseBody
    public Callable<Map<String, Object>> findByIdJson(final ArticleDto findArticle, final Model model) {
    return () -> {
    final ArticleDto article = ContextHolder.getBean(Article
    Service.class).findById(ArticleDto.class, findArticle);

    return ReturnDataUtils.newMap("article", article);
    };
    }

    /**
    * 文章：分页查询
    *
    * @param paginationArticle
    *            文章对象
    * @return 分页，json格式
    */
    @Deprecated
    @RequestMapping(value = { "/cms/article/paginationByLikeCode.json" })
    @ResponseBody
    public Callable<Map<String, Object>> paginationByLikeCodeJson(final ArticleDto paginationArticle) {
    return () -> {
    final PaginationDto
<ArticleDto> articlePagination = ContextHolder.getBean(Article
    Service.class).paginationByLikeCode(ArticleDto.class, paginationArticle);

    return ReturnDataUtils.newMap("articlePagination", articlePagination);
    };
    }

    /**
    * 文章：分页查询
    *
    * @param paginationArticle
    *            文章对象
    * @param model
    *            上下文映射
    */
    @Deprecated
    @RequestMapping(value = { "/cms/article/pagination.html" })
    public void paginationHtml(final ArticleDto paginationArticle, final Model model) {
    final PaginationDto
<ArticleDto> articlePagination = ContextHolder.getBean(Article
    Service.class).paginationByLikeCode(ArticleDto.class, paginationArticle);

    model.addAttribute("articlePagination", articlePagination);
    }

    /**
    * 文章：分页查询
    *
    * @param findArticle
    *            文章对象
    * @param model
    *            上下文映射
    */
    @Deprecated
    @RequestMapping(value = { "/cms/article/edit.html" })
    public void editHtml(final ArticleDto findArticle, final Model model) {
    final ArticleDto article = ContextHolder.getBean(Article
    Service.class).findById(ArticleDto.class, findArticle);

    model.addAttribute("article", article);
    }

    /**
    * 文章：查询
    *
    * @param findListArticle
    *            文章对象
    * @return 文章对象集合，json格式
    */
    @Deprecated
    @RequestMapping(value = { "/cms/article/findListByLikeCode.json" })
    @ResponseBody
    public Callable<Map<String, Object>> findListByLikeCodeJson(final ArticleDto findListArticle) {
    return () -> {
    final List<ArticleDto> articleList = ContextHolder.getBean(Article
    Service.class).findListByLikeCode(ArticleDto.class, findListArticle);

    return ReturnDataUtils.newMap("article", articleList);
    };
    }

    /**
    * 文章：保存
    *
    * @param saveArticle
    *            文章对象
    * @return 处理结果，json格式
    */
    @Deprecated
    @RequestMapping(value = { "/cms/article/save.json" })
    @ResponseBody
    public Callable<Map<String, Object>> saveJson(final ArticleDto saveArticle) {
    return () -> {
    ContextHolder.getBean(ArticleService.class).saveByIdOrCode(ArticleDto.class, saveArticle);

    return ReturnDataUtils.newMap();
    };
    }
    }
